﻿using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class JLTVK02XS : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        DataRow dwMaster;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        JLTVK02XSData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        private DataRow dwUI;
        DataTable tableUI;
        private string fromdndate = "", enddndate = "";
        private string fromdate = "", enddate = ""; 
        #endregion
        public JLTVK02XS()
        {
            InitializeComponent();
            this.Name = "订单统计";           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            m_Data = new JLTVK02XSData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("POSTYPE", typeof(string));
            tableUI.Columns.Add("KRS", typeof(string));
            tableUI.Columns.Add("LFD", typeof(string));
            tableUI.Columns.Add("CUPO", typeof(string));
            tableUI.Columns.Add("STAFF", typeof(string));
            tableUI.Columns.Add("COLL", typeof(string));
            tableUI.Columns.Add("ARTICLE", typeof(string));
            tableUI.Columns.Add("CUSTOMER", typeof(string));
            tableUI.Columns.Add("DESCR", typeof(string));
            tableUI.Columns.Add("FROMDNDATE", typeof(string));
            tableUI.Columns.Add("ENDDNDATE", typeof(string));
            tableUI.Columns.Add("FROMDATE", typeof(string));
            tableUI.Columns.Add("ENDDATE", typeof(string));
        }
        public override void OnWorkflowIDChange(string wfid)
        {
            base.OnWorkflowIDChange(wfid);
            try
            {
                dwWFEvent = null;
                DataSet SResult = m_Data.INTEXDBC.QueryICC_WFEVENTS(wfid, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwWFEvent = row;
                }
                if (dwWFEvent == null)
                    return;
                string events = dwWFEvent["WFEVENTS"].ToString().Trim();
                switch (events)
                {
                    case "A001021"://主面料已经审核，辅料可以采购
                      
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding(); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    string POSTYPE = txtPOSTYPE.Text.Trim();
                    string KRS = txtKRS.Text.Trim();
                    string LFD = m_Data.Right("000000" + txtLFD.Text.Trim(), 6);
                    string CUSTOMER = m_Data.Right("00000000" + txtCUSTOMER.Text.Trim(), 8);
                    string CUPO = txtCUPO.Text.Trim();
                    string STAFF = txtSTAFF.Text.Trim();
                    string ARTICLE = txtARTICLE.Text.Trim();
                    string DESCR = txtDESCR.Text.Trim();
                    string COLL = txtCOLL.Text.Trim();
                    if (POSTYPE == "")
                    {
                        POSTYPE = "AAA";
                    }
                    if (KRS == "")
                    {
                        KRS = "AA";
                    }
                    if (CUPO == "")
                    {
                        CUPO = "AAAAAAAAAAAAAAA";
                    }
                    if (STAFF == "")
                    {
                        STAFF = "AAAAAAAA";
                    }
                    if (COLL == "")
                    {
                        COLL = "AAAAAA";
                    }
                    if (fromdndate == "")
                    {
                        fromdndate = "0";
                    }
                    if (enddndate == "")
                    {
                        enddndate = "99999999";
                    }
                    if (fromdate == "")
                    {
                        fromdate = "0";
                    }
                    if (enddate == "")
                    {
                        enddate = "99999999";
                    }
                    dwUI["POSTYPE"] = POSTYPE;
                    dwUI["KRS"] = KRS;
                    dwUI["LFD"] = LFD;
                    dwUI["CUSTOMER"] = CUSTOMER;
                    dwUI["CUPO"] = CUPO;
                    dwUI["STAFF"] = STAFF;
                    dwUI["COLL"] = COLL;
                    dwUI["ARTICLE"] = ARTICLE;
                    dwUI["DESCR"] = DESCR;
                    dwUI["FROMDNDATE"] = fromdndate;
                    dwUI["ENDDNDATE"] = enddndate;
                    dwUI["FROMDATE"] = fromdate;
                    dwUI["ENDDATE"] = enddate;
                    setResult = m_Data.BuildJLTVK02XSData(dwUI);
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["WASTAMM"];
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
                
        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new JLTVK02XSData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            JLTVK02XSReport m_rpt = new JLTVK02XSReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("OpenExcel");
        }

                
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }

        private void FROM_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdndate = "";
                fromdndate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddndate = "";
                enddndate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnVKPDetail_Click(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            MessageInfo msgs = new MessageInfo();
            msgs.Where = m_Data.UserID;
            msgs.What = "销售单明细";
            msgs.Details = "销售单明细";
            m_Data.INTEXBL.CheckEvents("A901001", dwMaster, "销售单明细", msgs);
            msgs.Where = m_Data.UserID;
            msgs.What = "核价单明细";
            msgs.Details = "核价单明细";
            m_Data.INTEXBL.CheckEvents("A901002", dwMaster, "核价单明细", msgs);
            msgs.Where = m_Data.UserID;
            msgs.What = "销售出货总结";
            msgs.Details = "销售出货总结";
            m_Data.INTEXBL.CheckEvents("A901003", dwMaster, "销售出货总结", msgs);
            msgs.Where = m_Data.UserID;
            msgs.What = "应收款明细";
            msgs.Details = "应收款明细";
            m_Data.INTEXBL.CheckEvents("A901004", dwMaster, "应收款明细", msgs);
        }

        private void MasterDg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwMaster = drv.Row as DataRow;
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    }
}
